using System.Collections.Generic;

namespace Nivot.PowerShell.WindowsMobile
{
    internal class WindowsMobileFolder : StoreItem<CEFolder>
    {
        private const string ParamLocal = "Local";

        internal WindowsMobileFolder(CEFolder folder)
            : base(folder)
        {
            this.RegisterSwitchParameter(StoreProviderMethods.InvokeDefaultAction, ParamLocal);

            // files
            RegisterAdder((CEFile target) => NativeObject.AddCEFile(target));
            RegisterRemover((CEFile target) => NativeObject.RemoveCEFile(target));

            // folders
            RegisterRemover((CEFolder target) => NativeObject.RemoveCEFolder(target));
        }

        internal void CreateFolder(string fullPath)
        {
            this.NativeObject.CreateCEFolder(fullPath);

            // pass-thru
            var item = Provider.InvokeProvider.Item.Get(fullPath)[0];
            Provider.WriteItemObject(item, fullPath, true);
        }

        public override string ChildName
        {
            get { return NativeObject.Name; }
        }

        public override bool IsContainer
        {
            get { return true; }
        }

        public override StoreItemOptions ItemOptions
        {
            get { return StoreItemOptions.ShouldPipeItem | StoreItemOptions.ShouldTabComplete; }
        }

        public override IEnumerator<IStoreItem> GetEnumerator()
        {
            foreach (CEFolder folder in NativeObject.Folders)
            {
                yield return new WindowsMobileFolder(folder);
            }
            foreach (CEFile file in NativeObject.Files)
            {
                yield return new WindowsMobileFile(file);
            }
        }
    }
}